package br.com.betioli.jme.games.sokobob.sprite;

import br.com.betioli.jme.games.sokobob.config.GameConfig;

/**
 * Define a sprite do ator Bob, o jogador principal. 
 * Nesta classe deverão ser mantidas características específicas ao BobActor,
 * como por exemplo a animação da sprite.
 */
public class BobActor extends Actor implements Runnable {

    private boolean animating;
    private volatile Thread animationThread;

    public BobActor() {
        super(GameConfig.BOB_FRAMES, GameConfig.SPRITE_WIDTH, GameConfig.SPRITE_HEIGHT);

        setFrameSequence(GameConfig.BOB_FRAME_INDEX);
        
        startAnimation();
    }

    public void startAnimation() {

        if (!animating) {
            animating = true;
            
            animationThread = new Thread(this, "BobActor");
            animationThread.start();
        }
    }

    public void run() {
        while (animating) {
            try {
                animationThread.sleep(250);
            } catch (InterruptedException e) {
                stopAnimation();
            }

            nextFrame();
        }
    }

    public void stopAnimation() {
        animating = false;
    }
}
